package com.lib.utils;

import android.content.ContentResolver;
import android.content.Context;
import android.database.Cursor;
import android.net.Uri;
import android.provider.MediaStore;

import java.util.ArrayList;
import java.util.Formatter;

/**
 * 媒体工具类,
 */
public class MediaUtils {

    public static ArrayList<PhotoBean> listPhotos(Context context) {

        Uri imageUri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
        ContentResolver contentResolver = context.getContentResolver();
        String[] projection = new String[]{
                MediaStore.Images.ImageColumns.DATA,
                MediaStore.Images.ImageColumns.DISPLAY_NAME,
                MediaStore.Images.ImageColumns.SIZE,
                MediaStore.Images.ImageColumns.DATE_ADDED
        };
        Cursor cursor = contentResolver.query(imageUri, projection, null, null,
                MediaStore.Images.Media.DATE_ADDED + " desc");

        if (cursor == null) {
            return null;
        } else if (cursor.getCount() == 0) {
            cursor.close();
            return null;
        }
        ArrayList<PhotoBean> list = new ArrayList<>();
        while (cursor.moveToNext()) {
            PhotoBean photo = new PhotoBean();
            photo.setPath(cursor.getString(cursor.getColumnIndex(MediaStore.Images.ImageColumns.DATA)));
            photo.setFileName(cursor.getString(cursor.getColumnIndex(MediaStore.Images.Media.DISPLAY_NAME)));
            photo.setSize(cursor.getString(cursor.getColumnIndex(MediaStore.Images.Media.SIZE)));
            photo.setDate(cursor.getString(cursor.getColumnIndex(MediaStore.Images.Media.DATE_ADDED)));

            list.add(photo);
        }
        cursor.close();
        return list;
    }

    /**
     * 扫描系统里面的音频文件，返回一个list集合
     */
    public static ArrayList<MusicBean> listMusic(Context context) {

        Cursor cursor = context.getContentResolver().query(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI,
                null, null, null, MediaStore.Audio.AudioColumns.IS_MUSIC);

        if (cursor == null) {
            return null;
        } else if (cursor.getCount() == 0) {
            cursor.close();
            return null;
        }

        ArrayList<MusicBean> list = new ArrayList<>();
        while (cursor.moveToNext()) {
            long size = cursor.getLong(cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.SIZE));
            if (size > 1000 * 800) {
                MusicBean song = new MusicBean();
                song.setSize(size);
                song.setAlbum(cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.ALBUM)));
                song.setPath(cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.DATA)));
                song.setDuration(cursor.getInt(cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.DURATION)));
                String name = cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.DISPLAY_NAME));
                String singer = cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.ARTIST));
                // 注释部分是切割标题，分离出歌曲名和歌手 （本地媒体库读取的歌曲信息不规范）
                if (name.contains("-")) {
                    String[] str = name.split("-");
                    song.setSinger(str[0]);
                    song.setName(str[1]);
                } else {
                    song.setSinger(singer);
                    song.setName(name);
                }
                list.add(song);
            }
        }
        // 释放资源
        cursor.close();
        return list;
    }

    public static ArrayList<VideoBean> listVideo(Context context) {

        String[] mediaColumns = {
                MediaStore.Video.Media.DISPLAY_NAME,
                MediaStore.Video.Media.DATA,
                MediaStore.Video.Media.DURATION,
                MediaStore.Video.Media.SIZE,
                MediaStore.Video.Media.MIME_TYPE,
                MediaStore.Video.Media.DESCRIPTION,
        };

        ContentResolver resolver = context.getContentResolver();
        Cursor cursor = resolver.query(MediaStore.Video.Media.EXTERNAL_CONTENT_URI,
                mediaColumns, null, null, null);

        if (cursor == null) {
            return null;
        } else if (cursor.getCount() == 0) {
            cursor.close();
            return null;
        }

        ArrayList<VideoBean> list = new ArrayList<>();
        while (cursor.moveToNext()) {
            VideoBean video = new VideoBean();
            video.setFullName(cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Video.Media.DISPLAY_NAME)));
            video.setPath(cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Video.Media.DATA)));
            video.setDuration(cursor.getInt(cursor.getColumnIndexOrThrow(MediaStore.Video.Media.DURATION)));
            video.setSize(cursor.getInt(cursor.getColumnIndexOrThrow(MediaStore.Video.Media.SIZE)));
            video.setMimeType(cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Video.Media.MIME_TYPE)));
            list.add(video);
        }
        cursor.close();
        return list;
    }

    /**
     * 将Long类型时间格式化成时间字符串
     */
    public static String formatTime(long time) {

        if (time / 1000 % 60 < 10) {
            return new Formatter().format("%d:0%d", time / 1000 / 60, time / 1000 % 60).toString();
        } else {
            return new Formatter().format("%d:%d", time / 1000 / 60, time / 1000 % 60).toString();
        }
    }

    public static class MusicBean {

        /**
         * 歌手
         */
        private String mSinger;
        /**
         * 歌曲名
         */
        private String mName;
        /**
         * 歌曲的地址
         */
        private String mPath;
        /**
         * 歌曲长度
         */
        private int mDuration;
        /**
         * 歌曲的大小
         */
        private long mSize;

        private String mAlbum;

        public String getSinger() {

            return mSinger;
        }

        public void setSinger(String singer) {

            mSinger = singer;
        }

        public String getName() {

            return mName;
        }

        public void setName(String name) {

            mName = name;
        }

        public String getPath() {

            return mPath;
        }

        public void setPath(String path) {

            mPath = path;
        }

        public int getDuration() {

            return mDuration;
        }

        public void setDuration(int duration) {

            mDuration = duration;
        }

        public long getSize() {

            return mSize;
        }

        public void setSize(long size) {

            mSize = size;
        }

        public String getAlbum() {

            return mAlbum;
        }

        public void setAlbum(String album) {

            mAlbum = album;
        }
    }

    public static class PhotoBean {

        private String mPath;
        private String mFileName;
        private String mSize;
        private String mDate;

        public String getPath() {

            return mPath;
        }

        public void setPath(String path) {

            mPath = path;
        }

        public String getFileName() {

            return mFileName;
        }

        public void setFileName(String fileName) {

            mFileName = fileName;
        }

        public String getSize() {

            return mSize;
        }

        public void setSize(String size) {

            mSize = size;
        }

        public String getDate() {

            return mDate;
        }

        public void setDate(String date) {

            mDate = date;
        }
    }

    public static class VideoBean {

        /**
         * 视频名
         */
        private String mFullName;
        /**
         * 视频的地址
         */
        private String mPath;
        /**
         * 视频长度
         */
        private int mDuration;
        /**
         * 视频的大小
         */
        private long mSize;

        private String mMimeType;

        public String getFullName() {

            return mFullName;
        }

        public void setFullName(String fullName) {

            mFullName = fullName;
        }

        public String getPath() {

            return mPath;
        }

        public void setPath(String path) {

            mPath = path;
        }

        public int getDuration() {

            return mDuration;
        }

        public void setDuration(int duration) {

            mDuration = duration;
        }

        public long getSize() {

            return mSize;
        }

        public void setSize(long size) {

            mSize = size;
        }

        public String getMimeType() {

            return mMimeType;
        }

        public void setMimeType(String mimeType) {

            mMimeType = mimeType;
        }
    }
}